package com.example.springboot_jdk8.gjgw;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONUtil;

import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

public class HrOrgDataQuery {

    public static void main(String[] args) throws SQLException {
        String likeName = "测试";
        List<Entity> entities = Db.use().query("select * from hr_org_data where org_name like '%" + likeName + "%'");
        List<HrOrgData> orgs = getHrOrgDatas(entities);

        //过滤出所有涉及的唯一组织id
        Set<String> collect1 = orgs.stream()
                .map(hrOrgData -> hrOrgData.getOrgMainkeyPath().split("/"))
                .collect(Collectors.toSet())
                .stream()
                .flatMap(Arrays::stream)
                .filter(StrUtil::isNotEmpty)
                .collect(Collectors.toSet());
        System.out.println(collect1);

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < collect1.size(); i++) {
            stringBuilder.append("? ,");
        }
        Object[] array = collect1.toArray();
        List<Entity> entities1 = Db.use().query("select * from hr_org_data where mainkey in ("+stringBuilder.substring(0,stringBuilder.length()-1)+")",array);

        List<HrOrgData> hrOrgDatas = getHrOrgDatas(entities1);
        ArrayList<HrOrgData> result = new ArrayList<>(hrOrgDatas.size());
        for (HrOrgData hrOrgData : hrOrgDatas) {
            //以当前为子,查找父
            String parentId = hrOrgData.getParentMainkey();
            String finalParentId = parentId;
            //过滤出父组织
            HrOrgData parentOrg = hrOrgDatas.stream()
                    .filter(hrOrgData1 -> hrOrgData1.getMainkey()
                    .equals(finalParentId)).findFirst().orElse(null);
            //没有parent,说明是最高层级
            if (parentOrg == null || parentId.equals("0")) {
                //添加至结果集中
                result.add(hrOrgData);
            }
            //将有父组织的组装
            if (parentOrg != null) {
                //将自己添加至父的children属性中
                parentOrg.addChild(hrOrgData);
            }
        }
        System.out.println(JSONUtil.toJsonStr(result));
    }

    /**
     * 寻找唯一父组织
     * @param hrOrgDatas
     * @param parentId
     * @return
     */
    private static HrOrgData getParentOrgData(List<HrOrgData> hrOrgDatas, String parentId) {
        HrOrgData parentOrg = hrOrgDatas.stream().filter(hrOrgData1 -> {
            return hrOrgData1.getMainkey().equals(parentId);}).findFirst().orElse(null);
        return parentOrg;
    }


    private static List<HrOrgData> getHrOrgDatas(List<Entity> entities) {
        ArrayList<Map<String, Object>> resultMap = new ArrayList<>();
        for (Entity entity : entities) {
            Map<String, Object> rowMap = new HashMap<String, Object>();
            for (String fieldName : entity.getFieldNames()) {
                Object o = entity.get(fieldName);
                String underlineCase = StrUtil.toCamelCase(fieldName);
                rowMap.put(underlineCase, o);
            }
            resultMap.add(rowMap);
        }
        List<HrOrgData> orgs = JSONUtil.toBean(JSONUtil.toJsonStr(resultMap), new TypeReference<List<HrOrgData>>() {
        }, false);
        return orgs;
    }

}
